package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.entity.*;
import com.woniu.entity.response.ResponseEntity;
import com.woniu.entity.vo.PageProApprove;
import com.woniu.entity.vo.PageUserApprove;
import com.woniu.entity.vo.ProjectProApproveVo;
import com.woniu.service.IProApproveService;
import com.woniu.service.IProjectService;
import com.woniu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
@Controller
@RequestMapping("/brain/proApprove")
public class ProApproveController {

    @Autowired
    private IProApproveService iProApproveService;
    @Autowired
    private IProjectService ProjectService;

    @Value("${proApprove.proApproveCredentialPath}")
    private String proApproveCredentialPath;

    @RequestMapping("/apply")
    public ResponseEntity addProApprove(@RequestBody ProApprove proApprove) {
        //先判断项目中有没有审批表，没有就新建，将项目数据放到审批表中
        Project project = proApprove.getProject();
        //project = ProjectService.getByProId(project.getProId());
        proApprove.setProject(project);
        project.setProStatus("r");
        if(iProApproveService.getProApproveByProId(project.getProId())==null){

        }



        Dept dept = new Dept();
        dept.setDeptId(proApprove.getProject().getUser().getDept().getDeptId());
        proApprove.setProject(project);
        proApprove.setDept(dept);

        proApprove.setAppProName(project.getProName());
        proApprove.setAppProContract(project.getContract());
        proApprove.setAppProEndTime(project.getProEndTime());
        proApprove.setAppPrePrice(project.getPrePrice());
        proApprove.setAppApplyTime(LocalDateTime.now().toString());
        //判断项目中是否存在状态为审批中r的审批表
        //没有状态为r的审批表
        if (iProApproveService.getProAppProcessingByProId(project.getProId()) == null) {
            //建一个
            proApprove.setAppProStatus("r");
            iProApproveService.add(proApprove);
        } else if (iProApproveService.getProAppProcessingByProId(project.getProId()) != null) {
            //不等于空，就将项目的数据update进审批中
            iProApproveService.add(proApprove);
        }
        return ResponseEntity.success(proApprove);
    }


    @RequestMapping("/update/{approveId}")
    public ResponseEntity updateProApprove(@RequestBody ProApprove proApprove,@PathVariable int approveId){
        proApprove.setProject(proApprove.getProject());
        proApprove.setDept(proApprove.getProject().getUser().getDept());
        proApprove.setAppProStatus("r");
        proApprove.setAppApplyTime(LocalDateTime.now().toString());
        iProApproveService.update(proApprove);
        return ResponseEntity.success(proApprove);
    }

    @RequestMapping("/listAll")
    public ResponseEntity showListAll(){
        List<ProApprove> proApproves = iProApproveService.getAll();
        return ResponseEntity.success(proApproves);
    }

    @RequestMapping("/listByCondition")
    public ResponseEntity showListByCondition(@RequestBody ProApprove proApprove){
        List<ProApprove> proApproves = iProApproveService.getByCondition(proApprove);
        return ResponseEntity.success(proApproves);
    }

    @RequestMapping("/pageList")
    public ResponseEntity getProApproveByPage(@RequestBody PageProApprove pageProApprove) {
        if (pageProApprove.getPage() == null || pageProApprove.getPage().equals(0)) {
            pageProApprove.setPage(1);
        }
        if (pageProApprove.getPageSize() == null || pageProApprove.getPageSize().equals(0)) {
            pageProApprove.setPageSize(10);
        }
        return ResponseEntity.success(iProApproveService.getByPage(pageProApprove.getProApprove(),pageProApprove.getPage(), pageProApprove.getPageSize()));
    }

    @RequestMapping("/list")
    public ResponseEntity showList(){
        List<ProApprove> proApproves = iProApproveService.getAll();
        return ResponseEntity.success(proApproves);
    }


    @RequestMapping("/listbyapproveid/{approveid}")
    public ResponseEntity getByProApproveId(@PathVariable("approveid") int approeid){
        ProApprove proApprove = iProApproveService.getByProApproveId(approeid);
        return ResponseEntity.success(proApprove);
    }


    @RequestMapping("/listByProId/{proid}")
    public ResponseEntity listByProId(@PathVariable("proid") int proid){
        List<ProApprove> proApproves = iProApproveService.getProApproveByProId(proid);
        return ResponseEntity.success(proApproves);
    }

    @RequestMapping("/listbycondition")
    public ResponseEntity getByCondition(@RequestBody ProApprove proApprove){
        List<ProApprove> proApproves = iProApproveService.getByCondition(proApprove);
        return ResponseEntity.success(proApproves);
    }

    @RequestMapping("/getNumsByProStatusAndProId")
    public ResponseEntity getNumsByProStatusAndProId(Project project, String appProStatus){
        Integer nums = iProApproveService.getNumsByProStatusAndProId(project,appProStatus);
        return ResponseEntity.success(nums);
    }

    @RequestMapping("/getProApproveByProStatusAndProId")
    public ResponseEntity getProApproveByProStatusAndProId(Project project, String appProStatus){
        List<ProApprove> proApproves = iProApproveService.getProApproveByProStatusAndProId(project,appProStatus);
        return ResponseEntity.success(proApproves);
    }

    //审核通过
    @RequestMapping("/pass")
    @Transactional
    public ResponseEntity pass(@RequestBody ProApprove proApprove){
        proApprove.setAppProStatus("y");
        proApprove.getProject().setProStatus("y");
        iProApproveService.update(proApprove);
        ProjectService.update(proApprove.getProject());
        return ResponseEntity.success(proApprove);
    }

    //审核未通过
    @RequestMapping("/unpass")
    @Transactional
    public ResponseEntity unpass(@RequestBody ProApprove proApprove){
        proApprove.setAppProStatus("n");
        proApprove.getProject().setProStatus("n");
        iProApproveService.update(proApprove);
        ProjectService.update(proApprove.getProject());
        return ResponseEntity.success(proApprove);
    }


    @RequestMapping("/getByuid/{uid}")
    public ResponseEntity getByUid(@PathVariable("uid") int uid){
        List<ProApprove> proApproves = iProApproveService.findByUId(uid);
        return ResponseEntity.success(proApproves);
    }

    //审批流程逻辑
    //
    //
    //          提交项目                                                    审批通过
    //  用户----------------》生成项目表，项目审批表数据------------》项目负责人------------》执行项目------》项目结束
    //                                                              |
    //                                                 未通过审批     |
    //                             修改项目表，重新提交<----------------
    //
    //
    //项目表共4种状态，r 提交未审批、y 审批成功项目执行、n 审批未通过，需要修改重新提交、e 项目结束
    //审批表有3种状态，r 待审批、y 审批通过、n 审批未通过
    //
    //状态变更节点说明：
    //提交项目，项目表 r  审批表  r
    //审批通过，项目表 y  审批表  y
    //审批未通过，项目表 n 审批表 n
    //重新提交，项目表 r  审批表  r
    //项目结束， 项目表e  审批表 y

    @RequestMapping("/resubmit")
    @Transactional
    public ResponseEntity resubmit(@RequestBody ProjectProApproveVo ppav){
        ProjectService.reSubmit(ppav.getProject());
        iProApproveService.reSubmitProApprve(ppav.getProApprove());
        return ResponseEntity.success(null,"提交成功");
    }


    //审核通过
    @RequestMapping("/apass")
    @Transactional
    public ResponseEntity apass(@RequestBody ProApprove proApprove){

        int proid = proApprove.getProject().getProId();
        String aatime = DateUtils.getDateTime();
        String afeedback = proApprove.getAppFeedback();
        iProApproveService.Apass(proid,aatime,afeedback);
        ProjectService.ppass(proid);

        return ResponseEntity.success(null);
    }

    //审核未通过
    @RequestMapping("/aunpass")
    @Transactional
    public ResponseEntity aunpass(@RequestBody ProApprove proApprove){

        int proid = proApprove.getProject().getProId();
        String aatime = DateUtils.getDateTime();
        String afeedback = proApprove.getAppFeedback();

        iProApproveService.Aunpass(proid,aatime,afeedback);
        ProjectService.ppass(proid);
        return ResponseEntity.success(null);
    }

}
